<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" v-show="showSearch" :inline="true">
      <el-form-item label="用户名" prop="username">
        <el-input
          v-model="queryParams.username"
          placeholder="请输入用户名"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="tel">
        <el-input
          v-model="queryParams.tel"
          placeholder="请输入手机号"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择"
          clearable
          size="small"
          style="width: 240px"
        >
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="dateRange"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <el-row>
      <el-col>
        <el-table v-loading="loading" :data="roleList">
          <el-table-column label="订单id" prop="id"  v-if="columns[0].visible"/>
          <el-table-column label="用户名" prop="username"  v-if="columns[1].visible"/>
          <el-table-column label="手机号" width="120" prop="tel"  v-if="columns[2].visible"/>
          <el-table-column label="矿池" prop="currency"  v-if="columns[3].visible"/>
          <el-table-column label="产品名称" width="200" show-overflow-tooltip prop="productName"  v-if="columns[4].visible"/>
          <el-table-column label="订单金额(USDT)" prop="investAmount"  v-if="columns[5].visible"/>
          <el-table-column label="购买算力(T)" prop="buyPower"  v-if="columns[6].visible"/>
          <el-table-column label="已封装算力(T)" prop="packagePower"  v-if="columns[7].visible"/>
          <el-table-column label="订单状态" prop="status" :formatter="statusFormat"  v-if="columns[8].visible"/>
          <el-table-column label="订单时间" width="180" align="center" prop="createTime"  v-if="columns[9].visible">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
            class-name="small-padding fixed-width"
            width="80">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
              >修改</el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="750px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="总封装算力" prop="packagePower">
              <el-input v-model="form.packagePower" placeholder="请输入总封装算力值"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {bzzPackageList, bzzPackageUpdate} from "@/api/biz"
import {isNumber} from '@/utils/validate'

export default {
  name: "BzzPackage",
  data() {
    const validPackagePower = (rule, value, callback) => {
      if (!isNumber(value)) {
        callback(new Error("请输入数字"));
      } else {
        callback();
      }
    };
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 角色表格数据
      roleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        status: '0',
        username: undefined,
        tel: undefined
      },
      // 表单参数
      form: {
        stashId:'',
        id:'',   //投资订单id
        packagePower:''  //已封装算力
      },
      statusOptions:[
        {
          dictValue:'',
          dictLabel:'全部'
        },
        {
          dictValue:'0',
          dictLabel:'准备中'
        },
        {
          dictValue:'1',
          dictLabel:'挖矿中'
        },
        {
          dictValue:'2',
          dictLabel:'已撤销'
        },
        {
          dictValue:'3',
          dictLabel:'已完成'
        },
        {
          dictValue:'4',
          dictLabel:'已违约'
        }
      ],
      // 列信息
      columns: [
        { key: 0, label: `订单id`, visible: true },
        { key: 1, label: `用户名`, visible: true },
        { key: 2, label: `手机号`, visible: true },
        { key: 3, label: `矿池`, visible: true },
        { key: 4, label: `产品名称`, visible: true },
        { key: 5, label: `订单金额(USDT)`, visible: true },
        { key: 6, label: `购买算力(T)`, visible: true },
        { key: 7, label: `封装算力(T)`, visible: true },
        { key: 8, label: `订单状态`, visible: true },
        { key: 9, label: `订单时间`, visible: true },
      ],
      // 表单校验
      rules: {
        packagePower: [
          { required: true, message: "算力值不能为空", trigger: "blur" },
          { required: true, validator: validPackagePower, trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      bzzPackageList(this.addDateRange(this.queryParams, this.dateRange)).then(
        response => {
          this.roleList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        stashId:'',
        id:'',   //投资订单id
        packagePower: ''  //已封装算力
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.form.stashId = row.stashId;
      this.form.id = row.id;
      this.form.packagePower = row.packagePower;
      this.open = true;
      this.title = "修改封装算力值";
    },

    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          bzzPackageUpdate(this.form).then(response => {
            this.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          });
        }
      });
    }
  }
};
</script>
<style  scoped lang="scss">
.tableImageStyle{
  width: 30px;
  vertical-align: middle;
}
.ant-upload-preview {
  background-color: #fff;
  .avatar-uploader {
    .upload-desc {
      position: absolute;
      bottom: 0;
      left: 0;
      color: #ccc;
      font-size: 8px;
      right: 0;
    }
  }
}
::v-deep .avatar-uploader .el-upload--text {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  margin-right: 20px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
::v-deep .avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
::v-deep .avatar-uploader-icon {
  font-size: 22px;
  color: #ccc;
  width: 108px;
  height: 108px;
  line-height: 108px;
  text-align: center;
}
::v-deep.avatar {
  width: 108px;
  height: 108px;
  display: block;
}
.showDisabledClas {
  .el-input.is-disabled .el-input__inner,  .el-textarea.is-disabled .el-textarea__inner{
    color: #606266;
  }
}
</style>
